
/*-----------------------------------------------------------------------------*
* REPLICATION for "Do Provinces that Win the Spanish Christmas Lottery 
Experience a Surge in  Incumbent Popularity? An Out-of-Sample Replication of 
Bagues and Esteve-Volart (2016)". JPE Micro
* Authors: Carolina Bernal, Donald Green, and David Vilalta

* What for: Code for Individual Support for the Incumbent Party from Survey 
Data -- Appendix Tables A19 to A29
* Do file's author: Carolina Bernal
**----------------------------------------------------------------------------*/

cls

global folder "/Users/carolinabernal/Library/CloudStorage/Dropbox/Spanish_Lottery/StataDos_RScripts/202406 Analysis/Ours_replication_pkg/NEW AND FINAL REPPKG"

cd "${folder}"


* Path to figures and tables folders: 
global tables "Results/Tables_Survey/"
global figures "Results/Figures_Survey/" 

**----------------------------------------------------------------------------**
*** APPENDIX TABLES 19: Effects of Last Year's Lottery Prizes on Survey Measures 
*** of Incumbent Party Support, by Data Source, Sample Period, and Survey Date
**----------------------------------------------------------------------------**


use "Data/B&EV_repData/database surveys voting final.dta", clear


drop if province==""
encode province, gen(prov_num)

preserve

import delimited "Data/Our_data/year_month_survey.csv", clear


tempfile year_survey

save `year_survey'

restore

** Merge in mapping of survey number and survey year/month
merge m:1 survey using `year_survey'
drop if _merge!=3 
drop _merge

gen yearmonth=ym(year, month)
format yearmonth %tm
label var yearmonth "Year-Month"

encode fixed, gen(fixed_num)

global individual_characteristics "i.municipality_size female age i.education i.status"

rename (top_prizes_gdp expenditure_gdp)(top_prizes_gdp_ours expenditure_gdp_ours) // I'm changing the variables' name temporarily, just to match the names in the table.

** Col 1, Appendix Table 19: 
eststo a_reppkg: areg vote_incumbent top_prizes_gdp_ours expenditure_gdp_ours i.survey $individual_characteristics, absorb(fixed_num) cluster(province)

estadd local SurveyFE "$\checkmark$"
estadd local ProvinceFE "$\checkmark$"
estadd local YearFE "$\times$"
estadd local PeriodFE "$\times$"
estadd local PeriodCtrols "$\times$"
estadd local Data "Repl.Pkg"
estadd local Sample "Pre-10"
estadd local Propensity "$\times$"
estadd local Estimation "OLS"
estadd local Outcome "All year"

** Note that this is province*year FE

* Run boottest to get the bootstrap confidence sets
boottest top_prizes_gdp_ours, cluster(prov_num) seed(12345)

gen sample_pkg = 1 if e(sample)==1

** Col 2, Appendix Table 19:  
eststo a_reppkg_Q1: areg vote_incumbent top_prizes_gdp_ours expenditure_gdp_ours i.survey $individual_characteristics if month<4, absorb(prov_num) cluster(province)

estadd local SurveyFE "$\checkmark$"
estadd local ProvinceFE "$\checkmark$"
estadd local YearFE "$\times$"
estadd local PeriodFE "$\times$"
estadd local PeriodCtrols "$\times$"
estadd local Data "Repl.Pkg"
estadd local Sample "Pre-10"
estadd local Propensity "$\times$"
estadd local Estimation "OLS"
estadd local Outcome "Q1"

** NOte that this is province*year FE

* Run boottest to get the bootstrap confidence sets
boottest top_prizes_gdp_ours, cluster(prov_num) seed(12345)

gen sample_pkgQ1 = 1 if e(sample)==1

rename (top_prizes_gdp_ours expenditure_gdp_ours) (top_prizes_gdp expenditure_gdp) // Just undoing the renaming 

preserve

keep if sample_pkg == 1
keep survey
duplicates drop 

tempfile survey_list
save `survey_list'

restore		

use "Data/Our_data/20240723_SpanishLottery_Complete_survey.dta", clear 

drop if province==""
encode province, gen(prov_num)

** Get list of surveys used in Replication package
merge m:1 survey using `survey_list', gen(reppkg_surveys)

decode fixed_effect_num, gen(fixed)

** Get period with 3 quarters from year t-1 + first quarter of year t. 
rename year year_original
split fixed, parse(-) limit(1) gen(year)
destring year1, replace

** Period control:
gen preperiod=1 if year_original<2010
replace preperiod=0 if year_original>=2010

global individual_characteristics "i.municipality_size female age i.education i.status"
global ind_char_preperiod "preperiod#i.municipality_size preperiod#female preperiod#c.age preperiod#i.education preperiod#i.status"

** Col 3, Appendix Table 19: 
eststo b_preAll: areg vote_incumbent top_prizes_gdp_ours c.expenditure_gdp_ours##i.year_original i.year1 i.prov_num $individual_characteristics if year_original<2010, absorb(survey) 
estadd local SurveyFE "$\checkmark$"
estadd local ProvinceFE "$\checkmark$"
estadd local YearFE "$\checkmark$"
estadd local PeriodFE "$\times$"
estadd local PeriodCtrols "$\times$"
estadd local Data "Ours"
estadd local Sample "Pre-10"
estadd local Propensity "$\checkmark$"
estadd local Estimation "OLS"
estadd local Outcome "All year"

boottest top_prizes_gdp_ours, cluster(prov_num) seed(12345)

** Col 4, Appendix Table 19: 
eststo b_postAll: areg vote_incumbent top_prizes_gdp_ours c.expenditure_gdp_ours##i.year_original i.year1 i.prov_num $individual_characteristics if year_original>=2010, absorb(survey) 

estadd local SurveyFE "$\checkmark$"
estadd local ProvinceFE "$\checkmark$"
estadd local YearFE "$\checkmark$"
estadd local PeriodFE "$\times$"
estadd local PeriodCtrols "$\times$"
estadd local Data "Ours"
estadd local Sample "Post-09"
estadd local Propensity "$\checkmark$"
estadd local Estimation "OLS"
estadd local Outcome "All year"

boottest top_prizes_gdp_ours, cluster(prov_num) seed(12345)

** Col 5, Appendix Table 19:  
eststo b_pooledAll: areg vote_incumbent top_prizes_gdp_ours c.expenditure_gdp_ours##i.year_original i.prov_num preperiod $individual_characteristics $ind_char_preperiod i.prov_num#preperiod, absorb(survey)  

estadd local SurveyFE "$\checkmark$"
estadd local ProvinceFE "$\checkmark$"
estadd local YearFE "$\checkmark$"
estadd local PeriodFE "$\checkmark$"
estadd local PeriodCtrols "$\checkmark$"
estadd local Data "Ours"
estadd local Sample "Pooled"
estadd local Propensity "$\checkmark$"
estadd local Estimation "OLS"
estadd local Outcome "All year"

boottest top_prizes_gdp_ours, cluster(prov_num) seed(12345)


**** Build Appendix Table 19: 
esttab a_reppkg a_reppkg_Q1 b_preAll b_postAll b_pooledAll using ${tables}Survey_Appendix_Table_19.tex, ///
		keep(top_prizes_gdp_ours) nocon r2 nostar ///		
		cells(b(fmt(3)) se(fmt(3) par) ci(fmt(3) par) CIstr(fmt(%-18s))) ///
		mtitles se mtitles("Vote Incumb." "Vote Incumb." "Vote Incumb." "Vote Incumb." "Vote Incumb.")  ///
		coeflabels(top_prizes_gdp_ours "Top Lottery prizes" expenditure_gdp_ours "Lottery expenditure") ///
		scalars("Estimation Estimation" "Data Data" "Sample Sample" "Outcome Time Outcome" "ProvinceFE Province FE" "YearFE Year FE" "SurveyFE Survey FE" "PeriodFE Period FE" "PeriodCtrols Period*Controls"  "Propensity Propensity corrected")  replace	
		
/* NOTE: The WCB confidence sets are manually added to the tables from the Stata output 
generated here.
*/		



**----------------------------------------------------------------------------**
*** APPENDIX TABLE 20 & 21: Effects of Last Year's Lottery Prizes on Survey 
*** Measures of Incumbent Party Support by GENDER
**----------------------------------------------------------------------------**

use "Data/Our_data/20240723_SpanishLottery_Complete_survey.dta", clear 

drop if province==""
encode province, gen(prov_num)

decode fixed_effect_num, gen(fixed)

** Get period with 3 quarters from year t-1 + first quarter of year t. 
rename year year_original
split fixed, parse(-) limit(1) gen(year)
destring year1, replace

** Period control:
gen preperiod=1 if year_original<2010
replace preperiod=0 if year_original>=2010

global individual_characteristics "i.municipality_size age i.education i.status"
*global individual_characteristics "i.municipality_size female age i.education i.status"

global ind_char_preperiod "preperiod#i.municipality_size preperiod#c.age preperiod#i.education preperiod#i.status"
*global ind_char_preperiod "preperiod#i.municipality_size preperiod#female preperiod#c.age preperiod#i.education preperiod#i.status"


* Loop over female==0 (male) and female==1 (female)

foreach gender in 0 1 {
    
    * Local label depending on value of female
    if `gender'==0 local genderlabel male
    else local genderlabel female
    
    * Restrict sample
    preserve
        keep if month<4 & female==`gender'
        
        ** Col 4, Appendix Tables 20, 21: 
		eststo b_preQ1: areg vote_incumbent top_prizes_gdp_ours c.expenditure_gdp_ours##i.year_original i.prov_num $individual_characteristics if year_original<2010, absorb(survey) 

		estadd local SurveyFE "$\checkmark$"
		estadd local ProvinceFE "$\checkmark$"
		estadd local YearFE "$\checkmark$"
		estadd local PeriodFE "$\times$"
		estadd local PeriodCtrols "$\times$"
		estadd local Data "Ours"
		estadd local Sample "Pre-10"
		estadd local Propensity "$\checkmark$"
		estadd local Estimation "OLS"
		estadd local Outcome "Q1"

		boottest top_prizes_gdp_ours, cluster(prov_num) seed(12345)


		** Col 5, Appendix Tables 20, 21:
		eststo b_postQ1: areg vote_incumbent top_prizes_gdp_ours c.expenditure_gdp_ours##i.year_original i.prov_num $individual_characteristics if year_original>=2010, absorb(survey) 

		estadd local SurveyFE "$\checkmark$"
		estadd local ProvinceFE "$\checkmark$"
		estadd local YearFE "$\checkmark$"
		estadd local PeriodFE "$\times$"
		estadd local PeriodCtrols "$\times$"
		estadd local Data "Ours"
		estadd local Sample "Post-09"
		estadd local Propensity "$\checkmark$"
		estadd local Estimation "OLS"
		estadd local Outcome "Q1"

		boottest top_prizes_gdp_ours, cluster(prov_num) seed(12345)


		** Col 6, Appendix Tables 20, 21: 
		eststo b_pooledQ1: areg vote_incumbent top_prizes_gdp_ours c.expenditure_gdp_ours##i.year_original i.prov_num preperiod $individual_characteristics $ind_char_preperiod i.prov_num#preperiod, absorb(survey) 

		estadd local SurveyFE "$\checkmark$"
		estadd local ProvinceFE "$\checkmark$"
		estadd local YearFE "$\checkmark$"
		estadd local PeriodFE "$\checkmark$"
		estadd local PeriodCtrols "$\checkmark$"
		estadd local Data "Ours"
		estadd local Sample "Pooled"
		estadd local Propensity "$\checkmark$"
		estadd local Estimation "OLS"
		estadd local Outcome "Q1"

		boottest top_prizes_gdp_ours, cluster(prov_num) seed(12345)

		*** Appendix Tables 20, 21: Assemble tables 

		esttab b_preQ1 b_postQ1 b_pooledQ1 using ${tables}Survey_Appendix_Tables_20_21_`genderlabel'.tex, ///
				keep(top_prizes_gdp_ours) nocon r2 nostar ///		
				cells(b(fmt(3)) se(fmt(3) par) ci(fmt(3) par) CIstr(fmt(%-18s))) ///
				mtitles se mtitles("Vote Incumb." "Vote Incumb." ///
				"Vote Incumb.")  ///
				coeflabels(top_prizes_gdp_ours "Top Lottery prizes" expenditure_gdp_ours "Lottery expenditure") ///
				scalars("Estimation Estimation" "Data Data" "Sample Sample" "Outcome Time Outcome" "ProvinceFE Province FE" "YearFE Year FE" "SurveyFE Survey FE" "Propensity Propensity corrected")  		replace				
				
				
		/* NOTE: The WCB confidence sets are manually added to the tables from the Stata output generated here.	*/

    restore
}



**----------------------------------------------------------------------------**
*** APPENDIX TABLES 22 & 23: Effects of Last Year's Lottery Prizes on Survey 
*** Measures of Incumbent Party Support for Respondents Below and Above the 
*** Median Age (45 y/o)
**----------------------------------------------------------------------------**


use "Data/Our_data/20240723_SpanishLottery_Complete_survey.dta", clear 

drop if province==""
encode province, gen(prov_num)

decode fixed_effect_num, gen(fixed)

** Get period with 3 quarters from year t-1 + first quarter of year t. 
rename year year_original
split fixed, parse(-) limit(1) gen(year)
destring year1, replace

** Period control:
gen preperiod=1 if year_original<2010
replace preperiod=0 if year_original>=2010

global individual_characteristics "i.municipality_size female age i.education i.status"

global ind_char_preperiod "preperiod#i.municipality_size preperiod#female preperiod#c.age preperiod#i.education preperiod#i.status"


* Loop over age<45 (Below median age) and age>=45 (At or above median age)

foreach g in young old {
    
    preserve
        if "`g'" == "young" {
            keep if age < 45 & month<4
        }
        else if "`g'" == "old" {
            keep if age >= 45 & month<4
        }
        
		** Col 4, Appendix Tables 22 & 23: 
		eststo b_preQ1: areg vote_incumbent top_prizes_gdp_ours c.expenditure_gdp_ours##i.year_original i.prov_num $individual_characteristics if year_original<2010, absorb(survey) 

		estadd local SurveyFE "$\checkmark$"
		estadd local ProvinceFE "$\checkmark$"
		estadd local YearFE "$\checkmark$"
		estadd local PeriodFE "$\times$"
		estadd local PeriodCtrols "$\times$"
		estadd local Data "Ours"
		estadd local Sample "Pre-10"
		estadd local Propensity "$\checkmark$"
		estadd local Estimation "OLS"
		estadd local Outcome "Q1"

		boottest top_prizes_gdp_ours, cluster(prov_num) seed(12345)
		mean(vote_incumbent) if e(sample)==1

		** Col 5, Appendix Tables 22 & 23: 
		eststo b_postQ1: areg vote_incumbent top_prizes_gdp_ours c.expenditure_gdp_ours##i.year_original i.prov_num $individual_characteristics if year_original>=2010, absorb(survey) 

		estadd local SurveyFE "$\checkmark$"
		estadd local ProvinceFE "$\checkmark$"
		estadd local YearFE "$\checkmark$"
		estadd local PeriodFE "$\times$"
		estadd local PeriodCtrols "$\times$"
		estadd local Data "Ours"
		estadd local Sample "Post-09"
		estadd local Propensity "$\checkmark$"
		estadd local Estimation "OLS"
		estadd local Outcome "Q1"

		boottest top_prizes_gdp_ours, cluster(prov_num) seed(12345)
		mean(vote_incumbent) if e(sample)==1

		** Col 6, Appendix Tables 22 & 23: 
		eststo b_pooledQ1: areg vote_incumbent top_prizes_gdp_ours c.expenditure_gdp_ours##i.year_original i.prov_num preperiod $individual_characteristics $ind_char_preperiod i.prov_num#preperiod, absorb(survey) 

		estadd local SurveyFE "$\checkmark$"
		estadd local ProvinceFE "$\checkmark$"
		estadd local YearFE "$\checkmark$"
		estadd local PeriodFE "$\checkmark$"
		estadd local PeriodCtrols "$\checkmark$"
		estadd local Data "Ours"
		estadd local Sample "Pooled"
		estadd local Propensity "$\checkmark$"
		estadd local Estimation "OLS"
		estadd local Outcome "Q1"

		boottest top_prizes_gdp_ours, cluster(prov_num) seed(12345)
		mean(vote_incumbent) if e(sample)==1

		
		*** Appendix Tables 22, 23: Assemble tables 
		
		esttab b_preQ1 b_postQ1 b_pooledQ1 using ${tables}Survey_Appendix_Tables_22_23_`g'.tex, ///
			keep(top_prizes_gdp_ours) nocon r2 nostar ///		
			cells(b(fmt(3)) se(fmt(3) par) ci(fmt(3) par) CIstr(fmt(%-18s))) ///
			mtitles se mtitles("Vote Incumb." "Vote Incumb." ///
			"Vote Incumb.")  ///
			coeflabels(top_prizes_gdp_ours "Top Lottery prizes" expenditure_gdp_ours "Lottery expenditure") ///
			scalars("Estimation Estimation" "Data Data" "Sample Sample" "Outcome Time Outcome" "ProvinceFE Province FE" "YearFE Year FE" "SurveyFE Survey FE" "Propensity Propensity corrected")  replace				
			
				
		/* NOTE: The WCB confidence sets are manually added to the tables from the Stata output generated here.	*/

    restore
}


**----------------------------------------------------------------------------**
*** APPENDIX TABLES 24, 25, 26: Effects of Last Year's Lottery Prizes on Survey 
*** Measures of Incumbent Party Support for Respondents by Education Attainment
**----------------------------------------------------------------------------**


use "Data/Our_data/20240723_SpanishLottery_Complete_survey.dta", clear 

drop if province==""
encode province, gen(prov_num)

decode fixed_effect_num, gen(fixed)

** Get period with 3 quarters from year t-1 + first quarter of year t. 
rename year year_original
split fixed, parse(-) limit(1) gen(year)
destring year1, replace

** Period control:
gen preperiod=1 if year_original<2010
replace preperiod=0 if year_original>=2010

global individual_characteristics "i.municipality_size female age i.status"

global ind_char_preperiod "preperiod#i.municipality_size preperiod#female preperiod#c.age preperiod#i.status"


* Loop over education categories: education==1 (Primary education or below), education==2 (Secondary education), and education==3 (Higher education)

foreach e in 1 2 3 {
    
    * Define label depending on education value
    if `e' == 1 local edulabel "primary_or_below"
    else if `e' == 2 local edulabel "secondary"
    else if `e' == 3 local edulabel "higher"
    
    preserve
        keep if education == `e' & month<4
        
		** Col 4, Appendix Tables 24, 25, 26: 
		eststo b_preQ1: areg vote_incumbent top_prizes_gdp_ours c.expenditure_gdp_ours##i.year_original i.prov_num $individual_characteristics if year_original<2010, absorb(survey) 

		estadd local SurveyFE "$\checkmark$"
		estadd local ProvinceFE "$\checkmark$"
		estadd local YearFE "$\checkmark$"
		estadd local PeriodFE "$\times$"
		estadd local PeriodCtrols "$\times$"
		estadd local Data "Ours"
		estadd local Sample "Pre-10"
		estadd local Propensity "$\checkmark$"
		estadd local Estimation "OLS"
		estadd local Outcome "Q1"

		boottest top_prizes_gdp_ours, cluster(prov_num) seed(12345)
		mean(vote_incumbent) if e(sample)==1

		** Col 5, Appendix Tables 24, 25, 26: 
		eststo b_postQ1: areg vote_incumbent top_prizes_gdp_ours c.expenditure_gdp_ours##i.year_original i.prov_num $individual_characteristics if year_original>=2010, absorb(survey) 

		estadd local SurveyFE "$\checkmark$"
		estadd local ProvinceFE "$\checkmark$"
		estadd local YearFE "$\checkmark$"
		estadd local PeriodFE "$\times$"
		estadd local PeriodCtrols "$\times$"
		estadd local Data "Ours"
		estadd local Sample "Post-09"
		estadd local Propensity "$\checkmark$"
		estadd local Estimation "OLS"
		estadd local Outcome "Q1"

		boottest top_prizes_gdp_ours, cluster(prov_num) seed(12345)
		mean(vote_incumbent) if e(sample)==1

		** Col 6, Appendix Tables 24, 25, 26: 
		eststo b_pooledQ1: areg vote_incumbent top_prizes_gdp_ours c.expenditure_gdp_ours##i.year_original i.prov_num preperiod $individual_characteristics $ind_char_preperiod i.prov_num#preperiod, absorb(survey) 

		estadd local SurveyFE "$\checkmark$"
		estadd local ProvinceFE "$\checkmark$"
		estadd local YearFE "$\checkmark$"
		estadd local PeriodFE "$\checkmark$"
		estadd local PeriodCtrols "$\checkmark$"
		estadd local Data "Ours"
		estadd local Sample "Pooled"
		estadd local Propensity "$\checkmark$"
		estadd local Estimation "OLS"
		estadd local Outcome "Q1"

		boottest top_prizes_gdp_ours, cluster(prov_num) seed(12345)
		mean(vote_incumbent) if e(sample)==1

		
		*** Appendix Tables 24, 25, 26: Assemble tables 
		
		esttab b_preQ1 b_postQ1 b_pooledQ1 using ${tables}Survey_Appendix_Tables_24_25_26_`edulabel'.tex, ///
			keep(top_prizes_gdp_ours) nocon r2 nostar ///		
			cells(b(fmt(3)) se(fmt(3) par) ci(fmt(3) par) CIstr(fmt(%-18s))) ///
			mtitles se mtitles("Vote Incumb." "Vote Incumb." ///
			"Vote Incumb.")  ///
			coeflabels(top_prizes_gdp_ours "Top Lottery prizes" expenditure_gdp_ours "Lottery expenditure") ///
			scalars("Estimation Estimation" "Data Data" "Sample Sample" "Outcome Time Outcome" "ProvinceFE Province FE" "YearFE Year FE" "SurveyFE Survey FE" "Propensity Propensity corrected")  replace				
			
				
		/* NOTE: The WCB confidence sets are manually added to the tables from the Stata output generated here.	*/

    restore
}

**----------------------------------------------------------------------------**
*** APPENDIX TABLE 27: Survey Results Using Only the Last Year's Lottery Prize, 
*** by Year and Estimation Type (OLS by year, Meta-Analysis for pooled results), 
*** Using Our Data
**----------------------------------------------------------------------------**

use "Data/Our_data/20240723_SpanishLottery_Complete_survey.dta", clear 

drop if province==""
encode province, gen(prov_num)

decode fixed_effect_num, gen(fixed)

** Get period with 3 quarters from year t-1 + first quarter of year t. 
rename year year_original
split fixed, parse(-) limit(1) gen(year)
destring year1, replace


** Set of controls:
global individual_characteristics "i.municipality_size female age i.education i.status"
global ind_char_preperiod "preperiod#i.municipality_size preperiod#female preperiod#c.age preperiod#i.education preperiod#i.status"

** Models only for Q1:
keep if month<4


* Step 1: Initialize and Loop through All Years

local regression_names ""

* Initialize the matrix with 36 rows (1987-2022) and 6 columns (coefficients, lower bounds, upper bounds)
matrix bootstrap_results = J(36, 3, .) 

forvalues y = 1987/2022 {
    * Run the regression for the current year
    eststo boots_Q1_`y': reg vote_incumbent top_prizes_gdp_ours expenditure_gdp $individual_characteristics i.survey if year1 ==`y', cluster(prov_num)
		
	estadd local SurveyFE "$\checkmark$"
	estadd local Estimation "OLS"
	estadd local Data "Ours"
	estadd local Outcome "Q1"
	estadd local ProvinceFE "$\times$"
	estadd local Year "`y'"
	
	sum vote_incumbent if e(sample)==1
	
	
    * Run boottest for top_prizes_gdp
	boottest top_prizes_gdp_ours, cluster(prov_num) seed(12345)
	
	
    * Capture confidence set bounds from the r(CI) matrix
	matrix top_prizes_CI = r(CI)
    local top_prizes_ci_lower = top_prizes_CI[1,1]
    local top_prizes_ci_upper = top_prizes_CI[1,2]
		    
    
    * Calculate the row number based on the year
    local row = `y' - 1986  // 1987 starts at row 1, 1988 at row 2, etc.
    
    * Store the coefficients and both confidence interval bounds in the matrix
    matrix bootstrap_results[`row', 1] = _b[top_prizes_gdp_ours]
	matrix bootstrap_results[`row', 2] = `top_prizes_ci_lower'
    matrix bootstrap_results[`row', 3] = `top_prizes_ci_upper'

	    
    * Add regression name to the list
    local regression_names "`regression_names' boots_Q1_`y'"
	
	dis as error `y'
	
	tab survey if e(sample)==1
	
}

* Step 2: Save the Results to a New Dataset

* Double-check the matrix contents before conversion
matrix list bootstrap_results

* Save the results to a new dataset
svmat double bootstrap_results, names(col)
rename c1 top_prizes_coefficient
rename c2 top_prizes_ci_lower
rename c3 top_prizes_ci_upper

* Step 3: Clean the Dataset and Calculate Standard Errors

* Drop rows where the coefficients or confidence intervals are missing
drop if missing(top_prizes_coefficient) & missing(top_prizes_ci_lower) & missing(top_prizes_ci_upper) 

* Keep only the relevant variables for the meta-analysis
keep top_prizes_coefficient top_prizes_ci_lower top_prizes_ci_upper

* Generate a new year variable for the meta-analysis
gen year = 1987 + _n - 1  // Adjust the starting year if necessary

* Calculate standard errors from the confidence intervals
gen top_prizes_se = (top_prizes_ci_upper - top_prizes_ci_lower) / (2 * 1.96)

* Step 4: Meta-Analysis for top_prizes_gdp and expenditure_gdp

* Ensure that the year variable is correctly set and does not have duplicate values
sort year

*** Pooled:
* Meta-analysis for top_prizes_gdp
meta set top_prizes_coefficient top_prizes_se, studylabel(year)
meta summ

* Store the meta-analysis results for top_prizes_gdp as scalars
scalar top_prizes_meta_coef = r(theta)
scalar top_prizes_meta_se = r(se)

* Generate the forest plot after storing the results
meta forestplot, xline(0)
* Save the forest plot
graph export "${figures}Survey_Appendix_forestplot_top_prizes_pooled.png", replace

preserve 

keep if year<2010

*** Pre:
* Meta-analysis for top_prizes_gdp
meta set top_prizes_coefficient top_prizes_se, studylabel(year)
meta summ

* Store the meta-analysis results for top_prizes_gdp as scalars
scalar top_prizes_meta_coef = r(theta)
scalar top_prizes_meta_se = r(se)

* Generate the forest plot after storing the results
meta forestplot, xline(0)
* Save the forest plot
graph export "${figures}Survey_Appendix_forestplot_top_prizes_pre.png", replace


restore

preserve 

keep if year>=2010

**** Post:
* Meta-analysis for top_prizes_gdp
meta set top_prizes_coefficient top_prizes_se, studylabel(year)
meta summ

* Store the meta-analysis results for top_prizes_gdp as scalars
scalar top_prizes_meta_coef = r(theta)
scalar top_prizes_meta_se = r(se)

* Generate the forest plot after storing the results
meta forestplot, xline(0)
* Save the forest plot
graph export "${figures}Survey_Appendix_forestplot_top_prizes_post.png", replace

* Combine meta-analysis results into a single model -- place holder for meta analyses 
eststo meta_combined: estadd scalar top_prizes_gdp_ours = top_prizes_meta_coef
estadd scalar top_prizes_se = top_prizes_meta_se

restore

* Step 5: Generate LaTeX Tables. Appendix Table 27

*  Appendix Table 27. Part 1: years 1987 to 1996
esttab boots_Q1_1987 boots_Q1_1988 boots_Q1_1989 boots_Q1_1990 boots_Q1_1991 boots_Q1_1992 boots_Q1_1993 boots_Q1_1994 boots_Q1_1995 boots_Q1_1996  ///
    using ${tables}Survey_Appendix_Table_27_Part1_1987_1996.tex, ///
    keep(top_prizes_gdp_ours) nocon r2 nostar ///
    cells(b(fmt(3)) ci(fmt(3) par) CIstr(fmt(%-18s))) ///
    mtitles se mtitles("Vote Incumb." "Vote Incumb." "Vote Incumb." "Vote Incumb." "Vote Incumb." "Vote Incumb." "Vote Incumb." "Vote Incumb." "Vote Incumb." "Vote Incumb.")  ///
    coeflabels(top_prizes_gdp_ours "Top Lottery prizes")  ///
	scalars("Estimation Estimation" "Data Data" "Outcome Time Outcome" "ProvinceFE Province FE" "SurveyFE Survey FE" "Year Year") replace
	

* Appendix Table 27. Part 2: years 1997 to 2006
esttab boots_Q1_1997 boots_Q1_1998 boots_Q1_1999 boots_Q1_2000 boots_Q1_2001 boots_Q1_2002 boots_Q1_2003 boots_Q1_2004 boots_Q1_2005 boots_Q1_2006  ///
    using ${tables}Survey_Appendix_Table_27_Part2_1997_2006.tex, ///
    keep(top_prizes_gdp_ours) nocon r2 nostar ///
    cells(b(fmt(3)) ci(fmt(3) par) CIstr(fmt(%-18s))) ///
    mtitles se mtitles("Vote Incumb." "Vote Incumb." "Vote Incumb." "Vote Incumb." "Vote Incumb." "Vote Incumb." "Vote Incumb." "Vote Incumb." "Vote Incumb." "Vote Incumb.")  ///
    coeflabels(top_prizes_gdp_ours "Top Lottery prizes")  ///
	scalars("Estimation Estimation" "Data Data" "Outcome Time Outcome" "ProvinceFE Province FE" "SurveyFE Survey FE" "Year Year") replace


* Appendix Table 27. Part 3: years 2007 to 2016
esttab boots_Q1_2007 boots_Q1_2008 boots_Q1_2009 boots_Q1_2010 boots_Q1_2011 boots_Q1_2012 boots_Q1_2013 boots_Q1_2014 boots_Q1_2015 boots_Q1_2016  ///
    using ${tables}Survey_Appendix_Table_27_Part3_2007_2016.tex, ///
    keep(top_prizes_gdp_ours) nocon r2 nostar ///
    cells(b(fmt(3)) ci(fmt(3) par) CIstr(fmt(%-18s))) ///
    mtitles se mtitles("Vote Incumb." "Vote Incumb." "Vote Incumb." "Vote Incumb." "Vote Incumb." "Vote Incumb." "Vote Incumb." "Vote Incumb." "Vote Incumb." "Vote Incumb.")  ///
    coeflabels(top_prizes_gdp_ours "Top Lottery prizes")  ///
	scalars("Estimation Estimation" "Data Data" "Outcome Time Outcome" "ProvinceFE Province FE" "SurveyFE Survey FE" "Year Year") replace


* Appendix Table 27. Part 4: years 2017 to 2022 + meta analyses
esttab boots_Q1_2017 boots_Q1_2018 boots_Q1_2019 boots_Q1_2020 boots_Q1_2021 boots_Q1_2022 meta_combined meta_combined meta_combined ///
    using ${tables}Survey_Appendix_Table_27_Part4_2018_2022_meta.tex, ///
    keep(top_prizes_gdp_ours) nocon r2 nostar ///
    cells(b(fmt(3)) ci(fmt(3) par) CIstr(fmt(%-18s))) ///
    mtitles se mtitles("Vote Incumb." "Vote Incumb." "Vote Incumb." "Vote Incumb." "Vote Incumb." "Vote Incumb." "Vote Incumb." "Vote Incumb." "Vote Incumb." "Vote Incumb.")  ///
    coeflabels(top_prizes_gdp_ours "Top Lottery prizes")  ///
	scalars("Estimation Estimation" "Data Data" "Outcome Time Outcome" "ProvinceFE Province FE" "SurveyFE Survey FE" "Year Year") replace
	


/* NOTE: Information of the last 3 columns of the table (the meta-analysis in cols 37-39) and 
WCB confidence sets are manually added to the tables from the Stata output generated here.
*/



**----------------------------------------------------------------------------**
*** APPENDIX TABLE 28: Survey Results Using Lottery Prizes Cumulated over 
*** Electoral Term, by Year and Estimation Type, Using Our Data. Outcomes 
*** measured only in the month before the election
**----------------------------------------------------------------------------**


use "Data/Our_data/20240723_SpanishLottery_Complete_survey.dta", clear 

drop if province==""
encode province, gen(prov_num)

decode fixed_effect_num, gen(fixed)

** Month (for which we have survery data) before the election: 
gen preelectoral = 1 if yearmonth==tm(1986m5) | yearmonth==tm(1989m9) | ///
yearmonth==tm(1993m5) | yearmonth==tm(1996m2) | yearmonth==tm(2000m2) | ///
yearmonth==tm(2004m1) | yearmonth==tm(2008m1) | yearmonth==tm(2011m7) | ///
yearmonth==tm(2015m10) | yearmonth==tm(2016m4)| yearmonth==tm(2019m3) 
** Merge based on yearmonth province. 

preserve

***

* read electoral data
use "Data/Our_data/20250531_SpanishLottery_Complete_province.dta", clear 

replace yearmonth = yearmonth
format yearmonth %tm
label var yearmonth "Year-Month"

/*This is to match month of election in electoral data to pre month in survey 
data. In any case, what I'm merging is the cumulative prize data */
replace yearmonth = tm(1986m5) if yearmonth == tm(1986m6)
replace yearmonth = tm(1989m9) if yearmonth == tm(1989m10)
replace yearmonth = tm(1993m5) if yearmonth == tm(1993m6)
replace yearmonth = tm(1996m2) if yearmonth == tm(1996m3)
replace yearmonth = tm(2000m2) if yearmonth == tm(2000m3)
replace yearmonth = tm(2004m1) if yearmonth == tm(2004m3)
replace yearmonth = tm(2008m1) if yearmonth == tm(2008m3)
replace yearmonth = tm(2011m7) if yearmonth == tm(2011m11)
replace yearmonth = tm(2015m10) if yearmonth == tm(2015m12)
replace yearmonth = tm(2016m4) if yearmonth == tm(2016m6)
replace yearmonth = tm(2019m3) if yearmonth == tm(2019m4)


keep top_prizes_gdp_term_2 expenditure_gdp_term_2 year month yearmonth province_num province 


** province var is already clean: lower cased.

tempfile electoral_mon

save `electoral_mon'

restore /*Load survey data back*/

***

** Keep only observations that correspond to the (available) month before election.
keep if preelectoral==1

merge m:1 yearmonth province using `electoral_mon'
drop if _merge!=3 /*1986, 2019-11, 2023 didn't merge b/c no survey data for them */

** Get period with 3 quarters from year t-1 + first quarter of year t. 
rename year year_original
split fixed, parse(-) limit(1) gen(year)
destring year1, replace

global individual_characteristics "i.municipality_size female age i.education i.status"

** Row number of matrix
gen row = 1 if year_original == 1989
replace row = 2 if year_original == 1990
replace row = 3 if year_original == 1993
replace row = 4 if year_original == 1996
replace row = 5 if year_original == 2000 
replace row = 6 if year_original == 2004
replace row = 7 if year_original == 2008
replace row = 8 if year_original == 2011
replace row = 9 if year_original == 2015
replace row = 10 if year_original == 2019

* Step 1: Initialize and Loop through All Years

local regression_names ""

* Initialize the matrix with 10 rows (electoral years from 1989 to 2019) and 6 columns (coefficients, lower bounds, upper bounds)
matrix bootstrap_results = J(31, 3, .) 

foreach y in 1989 1993 1996 2000 2004 2008 2011 2015 2016 2019 {
    * Run the regression for the current year
    eststo boots_pre_m_`y': reg vote_incumbent top_prizes_gdp_term_2 expenditure_gdp_term_2 $individual_characteristics i.survey if year_original ==`y', cluster(prov_num)
	

	* Labels:
	estadd local SurveyFE "$\checkmark$"
	estadd local Estimation "OLS"
	estadd local Data "Ours"
	estadd local Outcome "pre_m"
	estadd local ProvinceFE "$\times$"
	estadd local Year "`y'"
	
	sum vote_incumbent if e(sample)==1
	
    * Run boottest for top_prizes_gdp
	boottest top_prizes_gdp_term_2, cluster(prov_num) seed(12345)
	
    * Capture confidence sets from the r(CI) matrix (these are WCB):
	matrix top_prizes_CI = r(CI)
    local top_prizes_ci_lower = top_prizes_CI[1,1]
    local top_prizes_ci_upper = top_prizes_CI[1,2]
		    
    
    * Calculate the row number based on the year
    local row = `y' - 1988  // 1989 starts at row 1, 1993 at row 5, etc.
    
    * Store the coefficients and both confidence interval bounds in the matrix
    matrix bootstrap_results[`row', 1] = _b[top_prizes_gdp_term_2]
	matrix bootstrap_results[`row', 2] = `top_prizes_ci_lower'
    matrix bootstrap_results[`row', 3] = `top_prizes_ci_upper'

	    
    * Add regression name to the list
    local regression_names "`regression_names' boots_pre_m_`y'"
	
	dis as error `y'
	
	tab survey if e(sample)==1
	
}

* Step 2: Save the Results to a New Dataset

* Double-check the matrix contents before conversion
matrix list bootstrap_results

* Save the results to a new dataset
svmat double bootstrap_results, names(col)
rename c1 top_prizes_coefficient
rename c2 top_prizes_ci_lower
rename c3 top_prizes_ci_upper

* Step 3: Clean the Dataset and Calculate Standard Errors

* Drop rows where the coefficients or confidence intervals are missing
drop if missing(top_prizes_coefficient) & missing(top_prizes_ci_lower) & missing(top_prizes_ci_upper) 

* Keep only the relevant variables for the meta-analysis
keep top_prizes_coefficient top_prizes_ci_lower top_prizes_ci_upper

* Generate a new year variable for the meta-analysis
gen year = _n   // Adjust the starting year if necessary

replace year = 1989 if year == 1 
replace year = 1990 if year == 2
replace year =  1993 if year == 3
replace year = 1996 if year == 4
replace year =  2000 if year == 5
replace year =  2004 if year == 6
replace year = 2008 if year == 7
replace year =  2011 if year == 8
replace year = 2015 if year == 9
replace year = 2019 if year == 10

* Calculate standard errors from the confidence intervals
gen top_prizes_se = (top_prizes_ci_upper - top_prizes_ci_lower) / (2 * 1.96)

* Step 4: Meta-Analysis for top_prizes_gdp and expenditure_gdp

* Ensure that the year variable is correctly set and does not have duplicate values
sort year

*** Pooled:
* Meta-analysis for top_prizes_gdp
meta set top_prizes_coefficient top_prizes_se, studylabel(year)
meta summ

* Store the meta-analysis results for top_prizes_gdp as scalars
scalar top_prizes_meta_coef = r(theta)
scalar top_prizes_meta_se = r(se)

* Generate the forest plot after storing the results
meta forestplot, xline(0)
* Save the forest plot
graph export "${figures}Survey_Appendix_forestplot_term_prizes_pooled_premonth.png", replace

preserve 

keep if year<2010

*** Pre:
* Meta-analysis for top_prizes_gdp
meta set top_prizes_coefficient top_prizes_se, studylabel(year)
meta summ

* Store the meta-analysis results for top_prizes_gdp as scalars
scalar top_prizes_meta_coef = r(theta)
scalar top_prizes_meta_se = r(se)

* Generate the forest plot after storing the results
meta forestplot, xline(0)
* Save the forest plot
graph export "${figures}Survey_Appendix_forestplot_term_prizes_pre_premonth.png", replace


restore

preserve 

keep if year>=2010

**** Post:
* Meta-analysis for top_prizes_gdp
meta set top_prizes_coefficient top_prizes_se, studylabel(year)
meta summ

* Store the meta-analysis results for top_prizes_gdp as scalars
scalar top_prizes_meta_coef = r(theta)
scalar top_prizes_meta_se = r(se)

* Generate the forest plot after storing the results
meta forestplot, xline(0)
* Save the forest plot
graph export "${figures}Survey_Appendix_forestplot_term_prizes_post_premonth.png", replace

* Combine meta-analysis results into a single model -- place holder for meta analyses 
eststo meta_combined: estadd scalar top_prizes_gdp_term_2 = top_prizes_meta_coef
estadd scalar top_prizes_se = top_prizes_meta_se

restore

* Step 5: Generate LaTeX Tables. Appendix Table 28

* Appendix Table 28. Part  1: years 1989 to 2011:
esttab boots_pre_m_1989 boots_pre_m_1993 boots_pre_m_1996 boots_pre_m_2000 boots_pre_m_2004 boots_pre_m_2008 boots_pre_m_2011 ///
    using ${tables}Survey_Appendix_Table_28_Part1_1989_2011.tex, ///
    keep(top_prizes_gdp_term_2) nocon r2 nostar ///
    cells(b(fmt(3)) ci(fmt(3) par) CIstr(fmt(%-18s))) ///
    mtitles se mtitles("Vote Incumb." "Vote Incumb." "Vote Incumb." "Vote Incumb." "Vote Incumb." "Vote Incumb." "Vote Incumb.")  ///
    coeflabels(top_prizes_gdp_term_2 "Top Lottery prizes")  ///
	scalars("Estimation Estimation" "Data Data" "Outcome Time Outcome" "ProvinceFE Province FE" "SurveyFE Survey FE" "Year Year") replace
	

* Appendix Table 28. Part  2: years 2015 to 2019 + meta analyses
esttab boots_pre_m_2015 boots_pre_m_2016 boots_pre_m_2019 meta_combined meta_combined meta_combined ///
    using ${tables}Survey_Appendix_Table_28_Part2_2015_2019_meta.tex, ///
    keep(top_prizes_gdp_term_2) nocon r2 nostar ///
    cells(b(fmt(3)) ci(fmt(3) par) CIstr(fmt(%-18s))) ///
    mtitles se mtitles("Vote Incumb." "Vote Incumb." "Vote Incumb." "Vote Incumb." "Vote Incumb." "Vote Incumb.")  ///
    coeflabels(top_prizes_gdp_term_2 "Top Lottery prizes")  ///
	scalars("Estimation Estimation" "Data Data" "Outcome Time Outcome" "ProvinceFE Province FE" "SurveyFE Survey FE" "Year Year") replace
	


/* NOTE: Information of the last 3 columns of the table (the meta-analysis in cols 37-39) and 
WCB confidence sets are manually added to the tables from the Stata output  generated here.
*/


**----------------------------------------------------------------------------**
*** APPENDIX TABLE 29: Survey Results Using Lottery Prizes Cumulated over 
*** Electoral Term, by Year and Estimation Type, Using Our Data. Outcomes 
*** measured only in the first quarter of the year (Q1)
**----------------------------------------------------------------------------**


use "Data/Our_data/20240723_SpanishLottery_Complete_survey.dta", clear 

drop if province==""
encode province, gen(prov_num)

decode fixed_effect_num, gen(fixed)

preserve

*****

* read electoral data
use "Data/Our_data/20250531_SpanishLottery_Complete_province.dta", clear 


rename year year1

keep province year1 top_prizes_gdp_term_2 expenditure_gdp_term_2
duplicates drop /*To drop the second 2019 election since prizes are the same */


tempfile electoral_yearQ

save `electoral_yearQ'

restore /* Re load survey data */

** Get period with 3 quarters from year t-1 + first quarter of year t. 
rename year year_original
split fixed, parse(-) limit(1) gen(year)
destring year1, replace

* merge
merge m:1 year1 province using `electoral_yearQ' 
keep if _merge==3 
drop _merge

* replace with zeroes 
replace top_prizes_gdp_term_2=0 if month>3
replace expenditure_gdp_term_2=0 if month>3


drop if month>=4

* Step 1: Initialize and Loop through All Years

local regression_names ""

* Initialize the matrix with 10 rows (electoral years) and 3 columns (coefficients, lower bounds, upper bounds)
matrix bootstrap_results = J(31, 3, .) /*the matrix has 31 rows to solve pb storing results and avoiding conformability error */

foreach y in 1989 1993 1996 2000 2004 2008 2011 2015 2016 2019{
    * Run the regression for the current year
    eststo boots_Q1_`y': reg vote_incumbent top_prizes_gdp_term_2 expenditure_gdp_term_2 $individual_characteristics i.survey if year_original ==`y', cluster(prov_num)
	
	* Labels:
	estadd local SurveyFE "$\checkmark$"
	estadd local Estimation "OLS"
	estadd local Data "Ours"
	estadd local Outcome "Q1"
	estadd local ProvinceFE "$\times$"
	estadd local Year "`y'"
	
    * Run boottest for top_prizes_gdp
	boottest top_prizes_gdp_term_2, cluster(prov_num) seed(12345)
	
    * Capture confidence sets from the r(CI) matrix (these are WCB):
	matrix top_prizes_CI = r(CI)
    local top_prizes_ci_lower = top_prizes_CI[1,1]
    local top_prizes_ci_upper = top_prizes_CI[1,2]
		    
    
    * Calculate the row number based on the year
    local row = `y' - 1988  // 1989 starts at row 1, 1993 at row 5, etc.
    
    * Store the coefficients and both confidence interval bounds in the matrix
    matrix bootstrap_results[`row', 1] = _b[top_prizes_gdp_term_2]
	matrix bootstrap_results[`row', 2] = `top_prizes_ci_lower'
    matrix bootstrap_results[`row', 3] = `top_prizes_ci_upper'

	    
    * Add regression name to the list
    local regression_names "`regression_names' boots_Q1_`y'"
	
	dis as error `y'
	
	tab survey if e(sample)==1
	sum vote_incumbent if e(sample)==1
	
}

* Step 2: Save the Results to a New Dataset

* Double-check the matrix contents before conversion
matrix list bootstrap_results

* Save the results to a new dataset
svmat double bootstrap_results, names(col)
rename c1 top_prizes_coefficient
rename c2 top_prizes_ci_lower
rename c3 top_prizes_ci_upper

* Step 3: Clean the Dataset and Calculate Standard Errors

* Drop rows where the coefficients or confidence intervals are missing
drop if missing(top_prizes_coefficient) & missing(top_prizes_ci_lower) & missing(top_prizes_ci_upper) 

* Keep only the relevant variables for the meta-analysis
keep top_prizes_coefficient top_prizes_ci_lower top_prizes_ci_upper

* Generate a new year variable for the meta-analysis
gen year = _n   // Adjust the starting year if necessary

replace year = 1989 if year == 1 
replace year = 1990 if year == 2
replace year =  1993 if year == 3
replace year = 1996 if year == 4
replace year =  2000 if year == 5
replace year =  2004 if year == 6
replace year = 2008 if year == 7
replace year =  2011 if year == 8
replace year = 2015 if year == 9
replace year = 2019 if year == 10

* Calculate standard errors from the confidence intervals
gen top_prizes_se = (top_prizes_ci_upper - top_prizes_ci_lower) / (2 * 1.96)

* Step 4: Meta-Analysis for top_prizes_gdp and expenditure_gdp

* Ensure that the year variable is correctly set and does not have duplicate values
sort year


*** Pooled:
* Meta-analysis for top_prizes_gdp
meta set top_prizes_coefficient top_prizes_se, studylabel(year)
meta summ

* Store the meta-analysis results for top_prizes_gdp as scalars
scalar top_prizes_meta_coef = r(theta)
scalar top_prizes_meta_se = r(se)

* Generate the forest plot after storing the results
meta forestplot, xline(0)
* Save the forest plot
graph export "${figures}Survey_Appendix_forestplot_term_prizes_pooled_Q1.png", replace

preserve 

keep if year<2010

*** Pre:
* Meta-analysis for top_prizes_gdp
meta set top_prizes_coefficient top_prizes_se, studylabel(year)
meta summ

* Store the meta-analysis results for top_prizes_gdp as scalars
scalar top_prizes_meta_coef = r(theta)
scalar top_prizes_meta_se = r(se)

* Generate the forest plot after storing the results
meta forestplot, xline(0)
* Save the forest plot
graph export "${figures}Survey_Appendix_forestplot_term_prizes_pre_Q1.png", replace


restore

preserve 

keep if year>=2010

**** Post:
* Meta-analysis for top_prizes_gdp
meta set top_prizes_coefficient top_prizes_se, studylabel(year)
meta summ

* Store the meta-analysis results for top_prizes_gdp as scalars
scalar top_prizes_meta_coef = r(theta)
scalar top_prizes_meta_se = r(se)

* Generate the forest plot after storing the results
meta forestplot, xline(0)
* Save the forest plot
graph export "${figures}Survey_Appendix_forestplot_term_prizes_post_Q1.png", replace

* Combine meta-analysis results into a single model -- place holder for meta analyses 
eststo meta_combined: estadd scalar top_prizes_gdp_term_2 = top_prizes_meta_coef
estadd scalar top_prizes_se = top_prizes_meta_se

restore

* Step 5: Generate LaTeX Tables. Appendix Table 29

* Appendix Table 29. Part  1: years 1989 to 2011:
esttab boots_Q1_1989 boots_Q1_1993 boots_Q1_1996 boots_Q1_2000 boots_Q1_2004 boots_Q1_2008 boots_Q1_2011 ///
    using ${tables}Survey_Appendix_Table29_1989_2011.tex, ///
    keep(top_prizes_gdp_term_2) nocon r2 nostar ///
    cells(b(fmt(3)) ci(fmt(3) par) CIstr(fmt(%-18s))) ///
    mtitles se mtitles("Vote Incumb." "Vote Incumb." "Vote Incumb." "Vote Incumb." "Vote Incumb." "Vote Incumb." "Vote Incumb.")  ///
    coeflabels(top_prizes_gdp_term_2 "Top Lottery prizes")  ///
	scalars("Estimation Estimation" "Data Data" "Outcome Time Outcome" "ProvinceFE Province FE" "SurveyFE Survey FE" "Year Year") replace
	

* Appendix Table 29. Part  2: years 2015 to 2019 + meta analyses
esttab boots_Q1_2015 boots_Q1_2016 boots_Q1_2019 meta_combined meta_combined meta_combined ///
    using ${tables}Survey_Appendix_Table29_2015_2019_meta.tex, ///
    keep(top_prizes_gdp_term_2) nocon r2 nostar ///
    cells(b(fmt(3)) ci(fmt(3) par) CIstr(fmt(%-18s))) ///
    mtitles se mtitles("Vote Incumb." "Vote Incumb." "Vote Incumb." "Vote Incumb." "Vote Incumb." "Vote Incumb.")  ///
    coeflabels(top_prizes_gdp_term_2 "Top Lottery prizes")  ///
	scalars("Estimation Estimation" "Data Data" "Outcome Time Outcome" "ProvinceFE Province FE" "SurveyFE Survey FE" "Year Year") replace
	


/* NOTE: Information of the last column of the table (the meta-analysis) and 
WCB confidence sets are manually added to the tables from the Stata output 
generated here.
*/
